import './index.scss'
import Header from '../../components/Header'
import Aside from '../../components/Aside'
import {
  getFeedbackConfigs,
  updateFeedbackConfig,
  creatFeedbackConfig,
  getFeedbackConfigDetail
} from '../../api/feedback'

export default {
  name: 'Setting',
  components: {
    Header,
    Aside
  },
  data() {
    return {
      showHeader: '',
      showSidebar: '',
      type: 1,
      title: '新增意见配置',
      feedbackConfigId: '',

      emails: '',
      mobiles: '',
      theme: '#409EFF'
    }
  },
  mounted() {
    if (this.$store.state.settings.showHeader == 'true') {
      this.showHeader = true
    } else {
      this.showHeader = false
    }
    if (this.$store.state.settings.showSidebar == 'true') {
      this.showSidebar = true
    } else {
      this.showSidebar = false
    }
    this.getFeedbackConfigs()
  },
  methods: {
    getFeedbackConfigs() {
      this.loading = true
      let params = {}
      getFeedbackConfigs(params, this)
        .then((res) => {
          if (res.data.code === 0) {
          } else {
            let data = res.data.content[0]
            this.feedbackConfigId = data.feedbackConfigId
            this.emails = data.emails
            this.mobiles = data.mobiles
            this.theme = data.theme
          }
          this.loading = false
        })
        .catch((error) => {
          this.loading = false
        })
    },

    submit() {
      let obj = {}
      obj.theme = this.theme
      if (this.emails) {
        if (this.emails.indexOf('，') !== -1) {
          return this.$message.warning('邮箱请使用英文逗号')
        } else {
          obj.emails = this.emails
        }
      } else {
        return this.$message.warning('请输入邮箱')
      }
      if (this.mobiles) {
        if (this.mobiles.indexOf('，') !== -1) {
          return this.$message.warning('邮箱请使用英文逗号')
        } else {
          obj.mobiles = this.mobiles
        }
      } else {
        return this.$message.warning('请输入手机号码')
      }
      let data = {
        data: obj
      }
      creatFeedbackConfig(data, this)
        .then((res) => {
          this.$message.success(res.data.message)
          if (res.data.code === '1') {
            this.getFeedbackConfigs()
          }
        })
        .catch((error) => {
          console.log(error.response)
          this.$message.warning(error.response.data.message)
        })
    },
    getFeedbackConfigDetail() {
      let params = {}
      getFeedbackConfigDetail(this.feedbackConfigId, params, this)
        .then((res) => {
          if (res.data.code === '1') {
            let data = res.data.content
            this.theme = data.theme
            this.emails = data.emails
            this.mobiles = data.mobiles
          }
        })
        .catch((error) => {
          console.log(error.response)
        })
    }
  },
  head() {
    return {
      title: '反馈设置 - EasyAPI意见反馈',
      meta: [
        { hid: 'description', name: 'description', content: '反馈设置' },
        { hid: 'keyword', name: 'keyword', content: '反馈设置' }
      ]
    }
  }
}
